/**
 * 
 */
package com.java.test.paymanage.mapper;

import com.java.test.paymanage.dto.PayTypeInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * @author Administrator
 *
 */
@Component
public class PayTypeMapper {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	public List<PayTypeInfo> selectpaytypeList() {

		String sql = "select * from paymanage_paytype";
		List<PayTypeInfo> list = jdbcTemplate.query(sql, new PayTypeInfo());
		return list;
	}

	public List<PayTypeInfo> selectPayTypeListByStatus() {

		String sql = "select * from paymanage_paytype t WHERE t.status = 1 order by t.sort desc";
		List<PayTypeInfo> list = jdbcTemplate.query(sql, new PayTypeInfo());
		return list;
	}

	public int insertpayType(PayTypeInfo info) {
		String sql = "insert into paymanage_paytype(pay_type_name,add_name,add_time,content,sort)values(?,?,?,?,?)";

		int i = jdbcTemplate.update(sql, info.getPayTypeName(), info.getAddName(), info.getAddTime(),
				info.getContent(),info.getSort());

		return i;

	}

	public int updatepayType(PayTypeInfo info) {
		String sql = "update paymanage_paytype set pay_type_name=?,content=? where id=?";

		int i = jdbcTemplate.update(sql, info.getPayTypeName(),
				info.getContent(),info.getId());

		return i;

	}

	
	public int updatepayTypestatusInfo(PayTypeInfo info) {
		String sql = "update paymanage_paytype set status=?  where id=?";

		int i = jdbcTemplate.update(sql, info.getStatus(),info.getId());

		return i;

	}
	
	
	public int deletepayType(PayTypeInfo info) {
		String sql = "delete from paymanage_paytype where id=?";

		int i = jdbcTemplate.update(sql, info.getId());

		return i;

	}

	public int updatepaysortType(List<PayTypeInfo> info) {
		String sql = "update paymanage_paytype set sort=? where id=?";
		jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
			public int getBatchSize() {
				return info.size();
				// 这个方法设定更新记录数，通常List里面存放的都是我们要更新的，所以返回list.size();
			}
			public void setValues(PreparedStatement ps, int i) throws SQLException {
				ps.setInt(1, info.get(i).getSort());
				ps.setInt(2, info.get(i).getId());
			}
		});

		return 1;
	}

	public PayTypeInfo selectById(String id) {
		String sql = "select * from paymanage_paytype where id=?";
		List<PayTypeInfo> list = jdbcTemplate.query(sql, new PayTypeInfo(), id);
		if(list.size()>0){
			return list.get(0);
		}
		return new PayTypeInfo();
	}

}
